package service;

import mapper.FloorMapper;
import mapper.SeatMapper;
import org.apache.ibatis.session.SqlSession;
import pojo.Floor;
import pojo.dto.FloorData;
import pojo.Seat;
import util.DBUtil;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

public class SeatService {
    /**
     * 获取所有楼层名
     *
     * @return List
     */
    public List<Floor> queryAllFloor() {
        SqlSession sqlSession = null;
        List<Floor> floorList = null;
        try {
            sqlSession = DBUtil.getSqlSession();
            FloorMapper mapper = sqlSession.getMapper(FloorMapper.class);
            floorList = mapper.queryAll();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
        return floorList;
    }

    public List<FloorData> queryAllFloorData() {
        SqlSession sqlSession = null;
        List<Floor> floorList = queryAllFloor();
        List<FloorData> floorDataList = null;
        try {
            sqlSession = DBUtil.getSqlSession();
            SeatMapper mapper = sqlSession.getMapper(SeatMapper.class);
            /* 获取楼层详细数据 */
            if (floorList != null) {
                for (Floor floor : floorList) {
                    Integer integer;
                    int totalNumber, occupiedNumber, closedNumber;

                    integer = mapper.getTotalNumber(floor.getName());
                    if (integer == null)
                        totalNumber = 0;
                    else
                        totalNumber = integer;

                    integer = mapper.getOccupiedNumber(floor.getName());
                    if (integer == null)
                        occupiedNumber = 0;
                    else
                        occupiedNumber = integer;

                    integer = mapper.getClosedNumber(floor.getName());
                    if (integer == null)
                        closedNumber = 0;
                    else
                        closedNumber = integer;

                    FloorData floorData = new FloorData(floor.getName(), totalNumber, occupiedNumber, closedNumber);
                    if (totalNumber != 0 && floorDataList == null)
                        floorDataList = new LinkedList<>();
                    floorDataList.add(floorData);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
        return floorDataList;
    }

    /**
     * 获取指定楼层的所有座位
     *
     * @param floorName 楼层名
     * @return List
     */
    public List<Seat> querySeatsByFloor(String floorName) {
        SqlSession sqlSession = null;
        List<Seat> seatList = null;
        try {
            sqlSession = DBUtil.getSqlSession();
            SeatMapper mapper = sqlSession.getMapper(SeatMapper.class);
            seatList = mapper.querySeatsByFloor(floorName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
        return seatList;
    }

    /**
     * 查询指定座位信息
     *
     * @param floorName 楼层名
     * @param number    座位号
     * @return Seat
     */
    public Seat querySeat(String floorName, int number) {
        SqlSession sqlSession = null;
        Seat seat = null;
        try {
            sqlSession = DBUtil.getSqlSession();
            SeatMapper mapper = sqlSession.getMapper(SeatMapper.class);
            seat = mapper.query(floorName, number);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
        return seat;
    }

    /**
     * 新增楼层
     *
     * @param floorName 楼层名
     * @return boolean
     */
    public boolean addFloor(String floorName) {
        SqlSession sqlSession = null;
        boolean result = false;
        try {
            sqlSession = DBUtil.getSqlSession();
            FloorMapper mapper = sqlSession.getMapper(FloorMapper.class);
            result = mapper.insert(floorName);
            sqlSession.commit();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
        return result;
    }

    /**
     * 新增单个座位（不单独使用）
     *
     * @param floorName 楼层名
     * @param number    座位号
     * @return boolean
     * @throws Exception
     */
    private boolean addSeat(String floorName, int number, SqlSession sqlSession) throws Exception {
        boolean result = false;
        try {
            sqlSession = DBUtil.getSqlSession();
            SeatMapper mapper = sqlSession.getMapper(SeatMapper.class);
            result = mapper.insert(floorName, number);
            sqlSession.commit();
        } catch (IOException e) {
            if (sqlSession != null)
                sqlSession.close();
            throw new RuntimeException(e);
        }
        return result;
    }

    /**
     * 该方法批量插入某个楼层的座位
     *
     * @param floorName 楼层名
     * @return
     */
    public boolean addSeatsBatch(String floorName, int count) {
        SqlSession sqlSession = null;
        try {
            sqlSession = DBUtil.getSqlSession();
            SeatMapper mapper = sqlSession.getMapper(SeatMapper.class);
            Integer integer = mapper.findMax(floorName);
            int maxNumber;
            if (integer == null)
                maxNumber = 0;
            else
                maxNumber = integer;
            for (int i = 1; i <= count; i++) {
                addSeat(floorName, maxNumber + i, sqlSession);
            }
            sqlSession.commit();
            return true;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
    }

    /**
     * 开放座位
     * @param floorName 区域名
     * @param number 座位号
     * @return boolean
     */
    public boolean openSeat(String floorName, int number) {
        boolean isSucceed = false;
        SqlSession sqlSession = null;
        try {
            sqlSession = DBUtil.getSqlSession();
            SeatMapper mapper = sqlSession.getMapper(SeatMapper.class);
            isSucceed = mapper.openSeat(floorName, number);
            sqlSession.commit();
            return isSucceed;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
    }

    /**
     * 关闭座位
     * @param floorName 区域名
     * @param number 座位号
     * @return boolean
     */
    public boolean closeSeat(String floorName, int number) {
        boolean isSucceed = false;
        SqlSession sqlSession = null;
        try {
            sqlSession = DBUtil.getSqlSession();
            SeatMapper mapper = sqlSession.getMapper(SeatMapper.class);
            isSucceed = mapper.closeSeat(floorName, number);
            sqlSession.commit();
            return isSucceed;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
    }
}